import { setErrorLog } from "./component/util.js"

import approveNumberList from './asset/approveNumber.json'     with { type: "json" }
import reportList from './asset/report.json'     with { type: "json" }
import oldReportList from './asset/oldReportList.json'     with { type: "json" }

// const result = {}
// const kingdee = result?.SaleOrderEntry?.map(item => ({
//     materialId: item.MaterialId.Number,
//     Fnumber: item.AuxPropId.F100001.Fnumber,
//     gysNumber: item.F_SYKA_gys.Number,
//     F_SYKA_semifinished: item.F_SYKA_semifinished,
//     F_SYKA_Date: item.F_SYKA_Date
// }))

function getArea(orderList, detailList) {
    const areaMap = {
        "浙江": "浙江乔治白服饰股份有限公司",
        "河南": "河南乔治服饰有限公司",
        "云南": "云南乔治白服饰股份有限公司"
    }
    const areaList = orderList.map(order => areaMap[detailList.find(detail => order['合同编号'] === detail.HTBN)['计划生产基地']])
    console.log(areaList.join('\n'))
}

function createMaterialUsage(data, productionOrderList) {
    const materialUsageList = []
    for (const productionOrder of productionOrderList) {
        const materialUsageTemplate = {
            productionOrderId: productionOrder['数据ID'],
            materialCode: '',
            times: 2,
            materialUseNum: 0,
            status: 1,
            bedId: 1
        }
        const auxiliaryMaterialList = data.auxiliaryMaterial.filter(item => item.订单子明细ID === productionOrder.订单明细ID)
        for (const auxiliaryMaterial of auxiliaryMaterialList) {
            const materialUsage = { ...materialUsageTemplate }
            materialUsage.materialCode = auxiliaryMaterial.货号
            materialUsage.materialUseNum = Math.floor(auxiliaryMaterial.用量 * (productionOrder.itemNum || productionOrder.totalNum) * 1000) / 1000
            materialUsageList.push(materialUsage)
        }
    }
    return materialUsageList
}

let runProductionOrderList = [
    {
        "数据ID": 713021,
        "code": "S.033723.03",
        "oldCode": "250906658",
        "totalNum": 607,
        "status": 1,
        "styleCode": "K.AFA.00395",
        "planStartDate": "2025-10-05",
        "planEndDate": "2025-10-15",
        "j_FID": null,
        "afterMeasureBodyDetailIds": [
            169776
        ]
    }
]
runProductionOrderList = runProductionOrderList.filter(item => {
    const oldCode = item.oldCode
    const totalNum = item.totalNum
    const hasFinishing = reportList.some(item => item['生产批号'] == oldCode && item.操作类型 === '后整理' && parseInt(item.完成数量) === totalNum)
    const stockIn = reportList.find(item => item['生产批号'] == oldCode && item.操作类型 === '入库')
    return hasFinishing && (!stockIn || parseInt(stockIn.完成数量) === 0)
})
console.log(runProductionOrderList)